package com.mi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mi.entity.MyPages;
import com.mi.entity.ProductEntity;
import com.mi.entity.UserEntity;
import com.mi.mapper.ProductMapper;
import com.mi.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class ProductServiceImpl implements ProductService {

    @Autowired(required = false)
    ProductMapper productMapper;

    //根据条件查询产品信息
    @Override
    public MyPages<ProductEntity> queryProducts(ProductEntity productEntity) {

        //设置当前页以及每页显示的记录数
        PageHelper.startPage(productEntity.getPageNum(),productEntity.getPageSize());

        List<ProductEntity> products = productMapper.queryProducts(productEntity);

        PageInfo<ProductEntity> pageInfo = new PageInfo<>(products);

        MyPages<ProductEntity> productPages = new MyPages<>();

        productPages.setList(pageInfo.getList());
        productPages.setPageNum(pageInfo.getPageNum());
        productPages.setPageSize(pageInfo.getPageSize());
        productPages.setPages(pageInfo.getPages());
        productPages.setPageCount((int)pageInfo.getTotal());

        return productPages;
    }

    //根据产品id查询产品信息
    @Override
    public ProductEntity queryProduct(int p_id) {
        return productMapper.queryProduct(p_id);
    }


    /**
     * 查询产品
     * @param productEntity
     * @return
     */
    public MyPages<ProductEntity> queryProductss(ProductEntity productEntity){
        PageHelper.startPage(productEntity.getPageNum(),productEntity.getPageSize());
        List<ProductEntity> productEntities = productMapper.queryProductss(productEntity);
        PageInfo<ProductEntity> pageInfo = new PageInfo<>(productEntities);
        MyPages<ProductEntity> productEntityMyPages = new MyPages<>();
        productEntityMyPages.setList(pageInfo.getList());
        productEntityMyPages.setPageNum(pageInfo.getPageNum());
        productEntityMyPages.setPageSize(pageInfo.getPageSize());
        productEntityMyPages.setPages(pageInfo.getPages());
        productEntityMyPages.setPageCount((int)pageInfo.getTotal());
        return productEntityMyPages;
    }

    /**
     * 查询单个产品
     * @param productEntity
     * @return
     */
    public ProductEntity  queryProduct(ProductEntity productEntity){
        List<ProductEntity> productEntities = productMapper.queryProductss(productEntity);
        if (productEntities==null||productEntities.size()==0){
            return null;
        }
        return productEntities.get(0);
    }

    /**
     * 修改产品
     * @param productEntity
     * @return
     */
    public boolean updProduct(ProductEntity productEntity){
        return productMapper.updProduct(productEntity);
    }

    /**
     * 删除产品
     * @param ids
     * @return
     */
    public boolean delProduct(String[] ids){
        return productMapper.delProduct(ids);
    }

    /**
     * 添加产品
     * @param productEntity
     * @return
     */
    public boolean addProduct(ProductEntity productEntity){
        return productMapper.addProduct(productEntity);
    }

    /**
     * 更改产品状态
     * @param productEntity
     * @return
     */
    public boolean updProductStatus(ProductEntity productEntity){
        return productMapper.updProductStatus(productEntity);
    }

    /**
     * 获取点击次数
     * @return
     */
    public List<ProductEntity> getCheckNumber(){
        PageHelper.startPage(1,10);
        return productMapper.getCheckNumber();
    }
    /**
     * 查看产品后浏览量加1
     * @param p_id
     * @return
     */
    @Override
    public boolean updProductCheck(int p_id) {
        return productMapper.updProductCheck(p_id);
    }

}
